/**
 * Created by zhanghe on 16/4/23.
 */
cur_result = {
    "data": [{
        "conment": "",
        "contestId": "",
        "createdTime": "",
        "categoryId": "",
        "id": "",
        "rank": 1,
        "resourceId": "",
        "userId": "",
        "stepId": "",
        "prizeName": "",
        "stepName": "",
        "prizeId": ""
    }],
    "showName": "",
}


navbarApp.controller('review_contest_controller', function ($scope, $http) {
    var contestCategoryId;
    $scope.select_category = function () {

        if (!stepID) {
            layer.msg('请先选择阶段');
            return;
        }
        $scope.cur_prizes = angular.copy($scope.result.prizes);

        for (var i = 0; i < $scope.cur_prizes.length; i++) {

            if ($scope.item.contestCategoryId != $scope.cur_prizes[i].contestCategoryId) {
                    $scope.cur_prizes.splice(i, 1);
                i--;
            }
        }
        contestCategoryId = $scope.item.contestCategoryId;
        $scope.getShowDetails();

    }
    var stepID = '';
    $scope.selected_step = function (oid,name) {
        stepID = oid;
        $scope.cur_name = name;
        if (!contestCategoryId) {
            return;
        }
        $scope.getShowDetails();

    }

    $scope.init = function (contestId) {
        var contestId = $.getUrlParam('contestId');
        if (!contestId) {
            $scope.result = cur_result;
            return false;
        }
        $.ajax({
            // 提交数据的类型 POST GET
            type: "post",
            // 提交的网址
            url: '/contests/contest/detail',
            // 提交的数据
            data: {
                contestId: contestId
            },
            dataType: "json",// "xml", "html", "script", "json",
            success: function (data) {
                if (data.status == '200') {
                    var cur_contest = data;

                    $scope.$apply(function () {
                        if (typeof(cur_contest) != 'undefined') {
                            if (cur_contest instanceof Array && cur_contest.length) {
                                $scope.result = cur_contest[0];
                            } else {
                                $scope.result = cur_contest.data;
                            }
                        }
                        $scope.item = {
                            contestCategoryId: ''
                        }
                    });


                } else if (data.status == '302') {
                    layer.msg('您没有该权限');
                } else {
                    layer.msg('服务器错误', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //do something

                    });
                }
            },
            error: function (e) {
            }
        });


    }

    $scope.getShowDetails = function () {
        //获取公示列表
        $.ajax({
            // 提交数据的类型 POST GET
            type: "post",
            // 提交的网址
            url: '/contests/contest/showDetails',
            // 提交的数据
            data: {
                stepId: stepID,
                categoryId: contestCategoryId
            },
            dataType: "json",// "xml", "html", "script", "json",
            success: function (data) {
                if (data.status == '200') {
                    var winning_works = data;

                    $scope.$apply(function () {
                        if (typeof(winning_works) != 'undefined') {
                            if (winning_works instanceof Array && winning_works.length) {
                                $scope.awards = winning_works[0];
                            } else {
                                $scope.awards = winning_works.data;
                                $scope.showName = winning_works.showName;

                            }
                            if (!$scope.awards.length) {
                                $scope.awards = cur_result.data;
                                $scope.showName = cur_result.showName;
                            }

                        }
                    });

                } else if (data.status == '302') {
                    layer.msg('您没有该权限');
                } else {
                    layer.msg('服务器错误', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //do something

                    });
                }
            },
            error: function (e) {
            }
        });
        //获取公示列表end

    }

    $scope.add_awards = function (index) {
        $scope.awards.splice(index + 1,0,{
            "conment": "",
            "contestId": "",
            "createdTime": "",
            "categoryId": "",
            "id": "",
            "rank": 1,
            "resourceId": "",
            "userId": "",
            "stepId": "",
            "prizeName": "",
            "stepName": "",
            "prizeId": ""
        });
    }
    $scope.remove_awards = function (index) {
        $scope.awards.splice(index, 1);
    }


    //提交配置保存
    $scope.btn_submit = function () {

        var contestId = $.getUrlParam('contestId');
        var dd = angular.copy($scope.awards);
        if (typeof(dd.state) == 'boolean') {
            if (dd.state) {
                dd.state = -10;
            } else {
                dd.state = 0;
            }
        }

        //去掉分类中为空的数据
        //for (var a in dd.contestCategorys) {
        //    if (!dd.contestCategorys[a].name) {
        //        dd.contestCategorys.splice(a, 1);
        //    }
        //}
        //for (var a in dd.contestSteps) {
        //    if (!dd.contestSteps[a].name) {
        //        dd.contestSteps.splice(a, 1);
        //    }
        //}

        for (var i = 0; i < dd.length; i++) {
            dd[i].showName = $scope.showName;
        }

        console.log(dd);


        layer.confirm('确定保存?', {icon: 3, title: '提示'}, function (index) {
            //do something
            $.ajax({
                // 提交数据的类型 POST GET
                type: "post",
                // 提交的网址
                url: '/contests/contest/updateShowDetails',
                // 提交的数据
                data: {
                    jsonData: sjApp.stringify({data: dd, showName: $scope.showName}),
                    contestId: contestId,
                    stepId: stepID,
                    categoryId: contestCategoryId
                },
                dataType: "json",// "xml", "html", "script", "json",
                success: function (data) {
                    if (data.status == '200') {
                        layer.msg('保存成功', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {

                            //do something
                            // $("#dataTable").dataTable().fnDraw();
                        });
                    } else if (data.status == '302') {
                        layer.msg('您没有该权限');
                    } else {
                        layer.msg('服务器错误', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something

                        });

                    }

                },
                error: function (e) {
                }
            });

            layer.close(index);
        });
    }


    $scope.init();


});